/**
 * Created by dukes on 2015/12/16.
 */
class MailScene extends eui.Component{
    public constructor(){
        super();
        this.skinName = "skins.scene.MailPanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }

    public updateMails(){
        if( this.listProvider)
            this.listProvider.source = Model.curMails;

        this.show_more.visible = Model.moreMails;
        if( Model.curMails.length > Model.MailPage){
            this.fuck_list.validateNow();
            // this.fuck_list.dataGroup.verticalScrollPosition = this.fuck_list.dataGroup.contentHeight - this.fuck_list.height;
        }
    }
    public childrenCreated(){
        super.childrenCreated();
        this.listProvider = new eui.ArrayCollection();
        this.fuck_list.dataProvider = this.listProvider;
        window["__mailList"](1,Model.MailPage);
    }

    public onReturn(evt:egret.TouchEvent){
        SceneMgr.getInstance().switchScene(SceneMgr.SCENE_MAIN);
    }

    public onMore(evt:egret.TouchEvent){
        var page:number = Math.ceil(Model.curMails.length/Model.MailPage);
        window["__mailList"](page+1,Model.MailPage);
    }

    private onAdded(evt:egret.Event){
        this.img_return.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onReturn, this);
        this.show_more.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onMore, this);
        if( Model.curMails)
            this.updateMails();
        RedPointMgr.getInstance().update(RedPointMgr.MAIL_ICON,false);
    }
    private onRemoved(evt:egret.Event){
        this.img_return.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onReturn, this);
        this.show_more.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onMore, this);
    }
    private listProvider:eui.ArrayCollection;
    public fuck_list:eui.List;
    private img_return:eui.Image;
    private show_more:eui.Image;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_return") {
            this.img_return = instance;
        } else if (name == "fuck_list") {
            this.fuck_list = instance;
            this.fuck_list.itemRenderer = MailItemRender;
        }else if (name == "show_more") {
            this.show_more = instance;
        }
    }
    private static _instance:MailScene;
    public static getInstance():MailScene{
        if( MailScene._instance == null)
            MailScene._instance = new MailScene();
        return MailScene._instance;
    }
}